<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormReles" label-width="">
      <el-form-item label="" prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入用户名称"></el-input>
      </el-form-item>
      <el-form-item label="" prop="password">
        <el-input v-model="loginForm.password" placeholder="请输入用户密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      msg: 'Welcome to login',
      loginForm: {
        username: '',
        password: ''
      },
      // 调用validate()，根据验证规则验证
      loginFormReles: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 5, message: '长度在2到5个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入用户密码', trigger: 'blur'},
          {min: 6, max: 13, message: '长度在6到13个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    onSubmit () {
      console.log('onSubmit ', JSON.stringify(this.loginForm))
      this.$refs.loginFormRef.validate(valid => {
        if (valid) {
          console.log('onSubmit--validate通过')
          this.$store.commit('SET_TOKEN', 'token')
          this.$router.push('/helloWorld')
        } else {
          console.log('onSubmit--validate不通过')
          this.$message.error('账号或密码错误')
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
